﻿Imports DbDataLib
Imports LibMultiPlug.Core.Plugins
Imports LibMultiPlug.Core.Plugins.Interfaces


Namespace Plugins

    Public Class PluginHandler


        Private _pluginManager As PluginManager


        Public Sub New()

            _pluginManager = PluginManager.GetInstance()

        End Sub


        Public Function StartTransaktion(ByVal transaktionName As String, ByVal args() As String) As Boolean

            Return _pluginManager.StartTransaktion(transaktionName, args)

        End Function


        Public Sub LoadPlugins(ByVal dockPanel As WeifenLuo.WinFormsUI.Docking.DockPanel, _
                               ByVal serviceManager As LibMultiPlug.Core.Services.ServiceManager, _
                               ByVal rootDataService As DbDataLib.DbDataService)

            _pluginManager.Environment = New PluginEnvironment(dockPanel, _
                                                               rootDataService, _
                                                               serviceManager)




            _pluginManager.LoadAll()

        End Sub

        Public Sub SetPlugins(ByVal moduleToolStrip As ToolStripMenuItem, _
                              ByVal toolStripContainer As ToolStripContainer, _
                              ByVal symbolleistenToolStrip As ToolStripMenuItem)

            If _pluginManager.LoadedPlugins.Count > 0 Then _
                symbolleistenToolStrip.DropDownItems.Add(New ToolStripSeparator)

            For Each p In _pluginManager.LoadedPlugins

                InternalSetPlugin(p, moduleToolStrip, toolStripContainer, symbolleistenToolStrip)

            Next

        End Sub

        Private Sub InternalSetPlugin(ByVal plugin As IPlugChildV1, _
                                      ByVal moduleToolStrip As ToolStripMenuItem, _
                                      ByVal toolStripContainer As ToolStripContainer, _
                                      ByVal toolBarStrip As ToolStripMenuItem)

            ' die module laden
            Dim menuItem As ToolStripMenuItem _
                = InternalLoadModulButtons(plugin, moduleToolStrip)

            ' symbolleiste laden
            If plugin.ToolStrip IsNot Nothing Then _
                Call InternalLoadToolBar(plugin, toolStripContainer, toolBarStrip)

            ' wenn es kinder gibt, dann sollen auch die geladen werden
            If plugin.Children IsNot Nothing Then _
                plugin.Children.ForEach(Sub(child As IPlugChildV1) _
                    InternalSetPlugin(child, menuItem, toolStripContainer, toolBarStrip))



        End Sub

        Private Sub InternalLoadToolBar(ByVal plugin As IPlugChildV1, _
                                        ByVal toolStripContainer As ToolStripContainer, _
                                        ByVal toolBarStrip As ToolStripMenuItem)

            Dim joined As Boolean = plugin.ToolStrip.Visible
            If plugin.ToolStrip.Visible Then _
                toolStripContainer.TopToolStripPanel.Join(plugin.ToolStrip, 1)

            Dim toolStripItem As ToolStripMenuItem _
                = toolBarStrip.DropDownItems.Add(plugin.ToolStrip.Text)
            toolStripItem.CheckOnClick = True
            toolStripItem.Checked = joined

            AddHandler toolStripItem.Click, Sub(sender As Object, e As EventArgs)
                                                If CType(sender, ToolStripMenuItem).Checked AndAlso _
                                                    Not toolStripContainer.Contains(plugin.ToolStrip) Then
                                                    toolStripContainer.TopToolStripPanel.Join(plugin.ToolStrip, 1)
                                                    plugin.ToolStrip.Visible = True
                                                Else
                                                    toolStripContainer.TopToolStripPanel.Controls.Remove(plugin.ToolStrip)
                                                    plugin.ToolStrip.Visible = False
                                                End If
                                            End Sub


        End Sub

        Private Function InternalLoadModulButtons(ByVal plugin As IPlugChildV1, _
                                                  ByVal moduleToolStrip As ToolStripMenuItem) As ToolStripMenuItem

            ' ein neuen button erstellen
            Dim menuItem As New ToolStripMenuItem(plugin.ActionText)

            ' wenn das icon vorhanden ist, dann dieses zum button hinzufügen
            If plugin.Icon IsNot Nothing Then
                menuItem.Image = plugin.Icon
            Else
                menuItem.Image = My.Resources.plugin
            End If

            ' die aktion beim klicken zuweisen
            AddHandler menuItem.Click, AddressOf plugin.Action

            ' hinzufügen des buttons
            moduleToolStrip.DropDownItems.Add(menuItem)

            Return menuItem

        End Function



    End Class

End Namespace
